package cn.com.xf.web.security;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import cn.com.xf.common.HttpUtils;
import cn.com.xf.utils.PasswordTools;
import cn.com.xf.web.XFContainsTool;

public class UserInfoService extends JdbcDaoSupport implements UserDetailsService {
	Log log = LogFactory.getLog(UserInfoService.class);

	private Map<String, UserInfo> userMap = null;
	public static String QUERY_BY_USERNAME = "select "
			+ "u.userid as userid,u.password as password,(case when u.enabled=1 then 0 else 1 end) as enabled,"
			+ "u.username as name,u.siteId as siteId,site.sitename as siteName,u.deptid as deptId,u.employid as employId "
			+ "from ts_user u left join ts_siteinfo site on u.siteid = site.siteid where u.userid=?";

	public UserInfoService() {
		/*
		 * userMap = new HashMap<String, UserInfo>(); UserInfo userInfo = null;
		 * List<GrantedAuthorityImpl> authList = new
		 * ArrayList<GrantedAuthorityImpl>(); authList.add(new
		 * GrantedAuthorityImpl("SYSTEM")); authList.add(new
		 * GrantedAuthorityImpl("MANAGE_SITE")); userInfo = new
		 * UserInfo("7690020488", "C5E4C16E03EA", true, true, true, true,
		 * authList); userInfo.setUserId("000001");
		 * userMap.put(userInfo.getUsername(), userInfo);
		 */
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
		UserDetails user = getUserByHttp(username);
		return user;
	}

	protected List<UserDetails> loadUsersByUsername(String username) {
		return getJdbcTemplate().query(QUERY_BY_USERNAME, new String[] { username }, new RowMapper<UserDetails>() {
			@Override
			public UserDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
				String username = rs.getString(1);
				String password = rs.getString(2);
				boolean enabled = rs.getBoolean(3);
				String name = rs.getString(4);
				String siteId = rs.getString(5);
				String siteName = rs.getString(6);
				String deptId = rs.getString(7);
				String employId = rs.getString(8);
				UserInfo user = new UserInfo(username, password, true, true, true, true,
						AuthorityUtils.NO_AUTHORITIES);
				user.setName(name);
				user.setSiteId(siteId);
				user.setSiteName(siteName);
				user.setDeptId(deptId);
				user.setEmployId(employId);
				return user;
			}
		});
	}

	public Map<String, UserInfo> getUserMap() {
		return userMap;
	}

	public void setUserMap(Map<String, UserInfo> userMap) {
		this.userMap = userMap;
	}

	private UserDetails getUserByHttp(String username) {
		String password = XFContainsTool.getPassword();
		String encodedPassword = PasswordTools.getEncodedPassword(password);
		List<UserDetails> userDetails = loadUsersByUsername(username);
		if (userDetails.size() > 0) {
			UserInfo userInfo = (UserInfo) userDetails.get(0);
			if (encodedPassword.equals(userInfo.getPassword())) {
				UserInfo user = new UserInfo(username, password, true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
				user.setName(userInfo.getName());
				user.setSiteId(userInfo.getSiteId());
				user.setSiteName(userInfo.getSiteName());
				user.setEmployId(userInfo.getEmployId());
				user.setDeptId(userInfo.getDeptId());
				return user;
			}
		}
		return null;

	}
}
